Declaring Objects

Objectives


Discussion

Objects and Classes

Dim objX as ClassName
objX = New ClassName("Constructor info here")
... OR ...
Dim objX as New ClassName("Constructor info here")

Creating and Displaying a Form

Dim frmX as Form2
frmX = New Form2()
frmX.Show()

  Back to top


Demonstration

1.  Open your Unit 2 project.  Add a new form and save it as FormX.vb.

2.  Add another form.  You can name it what you want.   Add a button (btnShowFormX) to this form.  Make this form the startup form.

3.  Open the code window for the form you created in Step 2.  Click on the "+" next to the statement "Windows Form Designer Generated Code."  Find the lines of code that declare and instantiates the button.  Remember that we can declare and instantiate an object in one or two lines of code as discussed above.  VS used two lines and automatically generated them for you.  You should find two lines that look like:

Friend WithEvents BtnShowFormX As System.Windows.Forms.Button

Me.BtnShowFormX = New System.Windows.Forms.Button()

This code creates a new button with a name btnShowFormX.  Also notice that they used Friend WithEvents instead of Dim .  This is another to declare an object. 

4.  Also notice that whenever we create a form, we are creating a class that defines the form. You can verify this by observing that the code for the form defines a class. Notice the top line in the code window which is Public Class YourFormName and the last line which is End Class  Everything that you add to the form is part of the definition of the class for this type of form.  If you want to actually use a form of this type in your program you have to instantiate it.  The one exception is the startup form.  VS handles the code for instantiating the startup form.

5.  Now  we will make a form of the FormX class.  Type the following in the click event for btnShowFormX.

Dim frmX as FormX
frmX = New FormX()
frmX.Show()

Understand that you could have used two lines to do this:

Dim frmX as New FormX()
frmX.Show()

6.  Run the program and click the button several times.  Everytime that you click the button, a new instance of FormX is created.

7.  After you create an object, you may need to get rid of it, or free up its memory.  You can do this by calling its Dispose method.  You may also be able to set an object to Nothing but the object may not always be released immediately.  For this course it really does not matter which approach you use but for more advanced programming you may need to understand this concept better.  The details of this issue are in the Help files referenced at the end of this lesson.  You should skim these references to get an idea of what the Nothing keyword does and its limitations.  

8.  Now let's check out Nothing .   Add the following line of code, frmX = Nothing to your code in the click event for btnShowFormX so that your click event looks like:

Dim frmX as FormX
frmX = New FormX()
frmX = Nothing
frmX.Show()

9.  Run your program and click the button. You should get an error because you have set the variable to Nothing before you try to use it.

10.  Now replace the line of code frmX = Nothing with frmX.Dispose() so that your event looks like:

Dim frmX as FormX
frmX = New FormX()
frmX.Dispose()
frmX.Show()

11.  Run your program. You should get an error again since you have disposed frmX before using it.

Back to top

Exercises

1.  Write a program that will have two forms (Main and GetNumbers).  Your program should meet the following requirements:

  1. frmMain should have a button (btnGetNumbers) and a label (lblTotal). 
  2. frmGetNumbers should have two textboxes (txtNum1 and txtNum2) and a button (btnClose)
  3. The form Main should be the startup form.
  4. When the user clicks btnGetNumbers, an instance of the form GetNumbers should be displayed.  You should probably use the Showdialog method but you should experiment with the Show method as well.
  5. After the user enters numbers in the textboxes and clicks btnGetNumbers the instance of GetNumbers should close and the sum of the numbers should be displayed in lblTotal on the form Main

Note that there is a standard approach for creating "Dialog box" forms.  We are not following it in this exercise but you may be interested. If so, search for "dialog boxes". 

2.  Modify your Main form in #1 as follows:

  1. Add a button (btnAddTextBox).
  2. When the user clicks the button, a new textbox will appear on the form.  You need to write code to declare and instantiate the textbox.  Then you will need to add the new textbox to the collection of controls on the form using code similar to "me.controls.add(myNewTextBox)".  You should look in the article "Adding Controls to Windows Forms" in Help to see how to do this.
Back to top
Links & Help
Back to top